﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XQL;
using System.IO;
using System.Xml;
using System.Security.AccessControl;

namespace XQL
{
    public partial class XQLAccessForm : Form
    {
        public XQLAccessForm()
        {
            InitializeComponent();
        }

        private void cnctBtn_Click(object sender, EventArgs e)
        {
            string ServerName = srvrName.Text;
            string AuthType = authType.Text;
            string UserName = usrName.Text;
            string Password = pwdTxt.Text;
            bool aType = false;

            if (ServerName == "")
            {
                MessageBox.Show("Please enter server name!");
                return;
            }
            if (AuthType == "")
            {
                MessageBox.Show("Please select authentication type!");
                return;
            }
            else
            {
                if (AuthType == "Windows Authentication")
                {
                    usrName.Text = "";
                    pwdTxt.Text = "";
                    usrName.Enabled = false;
                    pwdTxt.Enabled = false;
                    aType = true;
                }
                else if (UserName == "" && Password == "")
                {
                    MessageBox.Show("Please enter valid username and password!");
                    return;
                }
            }
            if (aType)
            {
                ServerName = ServerName.ToUpper();
                string localServerName = Environment.UserName.ToString().ToUpper() + "\\XQL";
                if (ServerName == localServerName)
                {
                    string localServerPath = Application.StartupPath;
                    string DirPath = localServerPath + "\\" + localServerName.Substring(0,localServerName.IndexOf("\\"));
                    if (!Directory.Exists(DirPath))
                    {
                        Directory.CreateDirectory(DirPath);
                        //DirectoryInfo d = new DirectoryInfo(DirPath);
                        //DirectorySecurity ds = d.GetAccessControl();
                        //ds.AddAccessRule(new FileSystemAccessRule(localServerName,FileSystemRights.FullControl,AccessControlType.Deny));
                        //d.SetAccessControl(ds);
                    }
                    this.Hide();
                    XQLManagerStudio xm = new XQLManagerStudio();
                    xm.Show();
                    xm.setPath(DirPath, localServerName.Substring(0, localServerName.IndexOf("\\")));
                }
                else
                    MessageBox.Show("Connection Failed");
            }
            else
            {
                MessageBox.Show("Remote Connection Feature is currently unavailable");
                return;
            }
            //XQLManager f1 = new XQLManager();
            //this.Hide();
            //f1.Show();
            
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void XQLAccessForm_Load(object sender, EventArgs e)
        {
            srvrName.Text = "gauthamr\\xql";
            authType.Text = "Windows Authentication";
            usrName.Enabled = false;
            pwdTxt.Enabled = false;
        }

        private void authType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (authType.Text == "Windows Authentication")
            {
                usrName.Enabled = false;
                pwdTxt.Enabled = false;
            }
            else
            {
                usrName.Enabled = true;
                pwdTxt.Enabled = true;
            }
        }
    }
}
